// pages/addAlbum/addAlbum.js.js
var app = getApp();
const qiniuUploader = require("../../lib/qiniuUploader");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AlbumName: '',
    PrewUrl: '../../images/img_add.png',
    cartArr: [{
        name: '公开',
        value: '1'
      },
      {
        name: '不公开',
        value: '0',
        checked: 'true'
      }
    ],
    IsPublic:'0',
    Content: '',
    Mark: '',
    Album:null,
    OPEN_ID:'',
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    var OPEN_ID_ = wx.getStorageSync('OPEN_ID')
    this.setData({
      OPEN_ID: OPEN_ID_
    });
    console.log(options.Album);
    if(options.Album !=null){
      this.setData({
        Album: JSON.parse(options.Album)
      });
      console.log(this.data.Album);
      if (this.data.Album != '') {
        this.setData({
          AlbumName: this.data.Album.AlbumName,
          PrewUrl: this.data.Album.CoverUrl,
          IsPublic: this.data.Album.IsPublic,
          Content: this.data.Album.Content,
          Mark: this.data.Album.Mark
        });
        if (this.data.Album.IsPublic == '0') {
          this.setData({
            cartArr: [{
              name: '公开',
              value: '1'
            },
            {
              name: '不公开',
              value: '0',
              checked: 'true'
            }
            ],
          });
        } else {
          this.setData({
            cartArr: [{
              name: '公开',
              value: '1',
              checked: 'true'
            },
            {
              name: '不公开',
              value: '0'
            }
            ],
          });
        }
      }
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  bindInput: function (event) {
    this.setData({
      AlbumName: event.detail.value,
    })
  },
  /**
  * radio监听事件
  */
  listenerRadioGroup: function (event) {
    console.log(event);
    this.setData({
      IsPublic: event.detail.value,
    })
  },
  bindTextAreaBlur: function (e) {
    console.log(e);
    this.setData({
      Content: e.detail.value
    })

  }, 
  uploadImg: function (event) {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#155D3E",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths);
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // 交给七牛上传
        qiniuUploader.upload(tempFilePaths[0], (res) => {
          that.setData({
            PrewUrl: res.imageURL,
          });
        }, (error) => {
          console.log('error: ' + error);
        }, {
            region: app.globalData.region,
            uploadURL: app.globalData.uploadURL,
            domain: app.globalData.domain,
            uptokenURL: app.globalData.uptokenURL,
          }, (res) => {
            console.log('上传进度', res.progress)
            console.log('已经上传的数据长度', res.totalBytesSent)
            console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
          });
      }
    })
  },
  submit: function (event) {
    console.log(this.data.AlbumName)
    console.log(this.data.PrewUrl)
    if (this.data.AlbumName == '') {
      wx.showToast({
        title: '请输入相册名称！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    } else if (this.data.PrewUrl == '../../images/img_add.png') {
      wx.showToast({
        title: '请上传相册封面！',
        image: '../../images/img_jinggao.png',
        duration: 1000
      })
    } else {
      if (this.data.Album !=null) {
        console.log(this.data.Album)
        wx.request({
          url: app.globalData.ServiceUrl + 'album/ModifyAlbum', //接口地址
          method: 'POST',
          data: {
            ValidateData: app.globalData.ValidateData,
            paras: {
              AlbumGuid: this.data.Album.AlbumGuid,
              AlbumName: this.data.AlbumName,
              IsPublic: this.data.IsPublic,
              Content: this.data.Content,
              Mark: this.data.Mark,
              OwnerUserGuid: this.data.OPEN_ID,
              CoverUrl: this.data.PrewUrl
            }
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
              wx.showToast({
                title: '修改相册成功！',
                icon: 'success',
                duration: 1000,
                complete: function () {
                  //上传成功，关闭页面
                  wx.navigateBack({})
                } //接口调用结束的回调函数  
              })
            } else {
              wx.showToast({
                title: '修改相册失败！',
                image: '../../images/img_jinggao.png',
                duration: 1000
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '新增相册失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000
            })
          }
        })
      }else{
        wx.request({
          url: app.globalData.ServiceUrl + 'album/SaveAlbum', //接口地址
          method: 'POST',
          data: {
            ValidateData: app.globalData.ValidateData,
            paras: {
              AlbumName: this.data.AlbumName,
              IsPublic: this.data.IsPublic,
              Content: this.data.Content,
              Mark: this.data.Mark,
              OwnerUserGuid: this.data.OPEN_ID,
              CoverUrl: this.data.PrewUrl
            }
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
              wx.showToast({
                title: '新增相册成功！',
                icon: 'success',
                duration: 1000,
                complete: function () {
                  //上传成功，关闭页面
                  wx.navigateBack({})
                } //接口调用结束的回调函数  
              })
            } else {
              wx.showToast({
                title: '新增相册失败！',
                image: '../../images/img_jinggao.png',
                duration: 1000
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '新增相册失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000
            })
          }
        })
      }
      
    }
  }
})